import React, { useState } from 'react';
import { DownloadOutlined, InboxOutlined } from '@ant-design/icons';
import { message, Upload, Select, Button } from 'antd';
import Url from '../api/url';
const { Dragger } = Upload;

const props = {
  name: 'file',
  multiple: false,
  method:"post",
  onChange(info) {
    const { status,response } = info.file;
    if (status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (status === 'done') {
      message.success(`上传成功`);
    } else if (status === 'error') {
      if(response && response.error && response.error.startsWith("Error: ORA-01861")){
          message.error(`日期格式错误,请转换成字符格式yyyy-mm-dd. `);
      }else{
        if(response &&  response.error){
          message.error(`上传失败: ${response.error}`);
        }else{
          message.error(`上传失败`);
        }
        
      }
    }
  },
  onDrop(e) {
    console.log('Dropped files', e.dataTransfer.files);
  },
};

const tableOptions = [
  { label: '销售流向', value: 'tb_flow_sales_records' },
  { label: '采购流向', value: 'tb_flow_pur_records',disabled:false },
  { label: '库存', value: 'tb_flow_store_records' ,disabled:true},
  { label: '批次库存', value: 'tb_flow_lotstore_records',disabled:true },
];

const ImportExcel = () => {
  const [table, setTable] = useState(tableOptions[0].value);

  const option = tableOptions.find(item => item.value === table);

  // 添加下载模板方法
  const downloadTemplate = () => {
    fetch(Url.baseURL + Url.downloadTemplate + `?table=${table}`, {
      method: 'GET',
      headers: {
        'authorization': localStorage.getItem('token')
      }
    })
    .then(response => response.blob())
    .then(blob => {
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = `${option.label}模板.xlsx`; 
      document.body.appendChild(a);
      a.click();
      window.URL.revokeObjectURL(url);
      document.body.removeChild(a);
    })
    .catch(error => {
      console.error('下载模板失败:', error);
      message.error('下载模板失败',error);
    });
  };


  // 动态action，带上表名参数
  const uploadProps = {
    ...props,
    action: Url.baseURL+Url.uploadExcel+`?table=${table}`,
    headers: {
      authorization: localStorage.getItem('token'),
    },
  };
  return (
    <div>
      <div style={{ marginBottom: 16 }}>
        <Select
          value={table}
          onChange={setTable}
          style={{ width: 200 }}
          options={tableOptions}
        />
                    <Button
  type="primary"
  icon={<DownloadOutlined />}
  onClick={downloadTemplate}
  style={{ 
    marginLeft: '10px',  // 靠右对齐
    backgroundColor: '#52c41a'  // 使用绿色
  }}
>
  下载导入模板
</Button>
      </div>

      <Dragger {...uploadProps}>
        <p className="ant-upload-drag-icon">
          <InboxOutlined />
        </p>
        <p className="ant-upload-text">单击或拖动文件到此区域进行上传</p>
        <p className="ant-upload-hint">
          只能上传xslx格式的excel。
        </p>
      </Dragger>



    </div>
  );
};



export default ImportExcel;




